<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'proj:contract:pmContract2:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="40%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProjContractPmContract2Form',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { PmContract2, pmContract2Save, pmContract2Form } from '/@/api/proj/contract/pmContract2';
  import { pmProjectinformationTreeData } from '/@/api/proj/info/pmProjectinformation';
  import { fa } from 'element-plus/es/locale';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('proj.contract.pmContract2');
  const { showMessage } = useMessage();
  const record = ref<PmContract2>({} as PmContract2);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增工程项目合同') : t('编辑工程项目合同'),
  }));

  const showCol = ref(false);

  const inputFormSchemas: FormSchema[] = [
  {
      label: t('项目名称'),
      field: 'prono',
      fieldLabel: 'proname',
      component: 'TreeSelect',
      componentProps: {
        api: pmProjectinformationTreeData,
        params: {},
        canSelectParent: false,
        allowClear: false,
      },
      itemProps: { validateTrigger: 'blur' },
      required: true,
    },
    {
      label: t('项目编号'),
      field: 'prono',
      component: 'Input',
      dynamicDisabled: true,
      componentProps: {
        maxlength: 50,
      },
      required: true,
    },
    
    {
      label: t('合同分类'),
      field: 'clsCode',
      component: 'Select',
      componentProps: {
        dictType: 'zf_constract_cls',
        allowClear: true,
      },
      required: true,
      itemProps: { validateTrigger: 'blur' },
    },
    {
      label: t('合同编号'),
      field: 'code',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
    },
    {
      label: t('合同名称'),
      field: 'name',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
      required: true,
    },
    {
      label: t('合同类型'),
      field: 'ctype',
      component: 'Select',
      componentProps: {
        dictType: 'zf_constract_type',
        allowClear: true,
        onChange: (v) => {
          if (v !== '3') {
            // setFieldsValue({
            //   isum: '0',
            //   isum2: '0',
            //   isum3: '0',
            // });
            showCol.value = false;
          } else {
            showCol.value = true;
          }
          updateSchema([
            { field: 'isum', show: showCol.value },
            { field: 'isum2', show: showCol.value },
            { field: 'isum3', show: showCol.value },
          ]);
        },
      },
      required: true,
      itemProps: { validateTrigger: 'blur' },
    },
    {
      label: t('合同金额'),
      field: 'isum',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
      show: showCol.value,
    },
    {
      label: t('安全文明措施费'),
      field: 'isum2',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
      show: showCol.value,
    },
    {
      label: t('监理费'),
      field: 'isum3',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
      show: showCol.value,
    },
    {
      label: t('附件上传'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'pmContract2_file',
        uploadType: 'all',
        isPreview: true,
      },
      colProps: { lg: 24, md: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await pmContract2Form(data);
    record.value = (res.pmContract2 || {}) as PmContract2;
    record.value.__t = new Date().getTime();
    if(record.value.ctype == '3'){
      showCol.value = true
    }else{
      showCol.value = false
    }
    updateSchema([
            { field: 'isum', show: showCol.value },
            { field: 'isum2', show: showCol.value },
            { field: 'isum3', show: showCol.value },
            { field: 'prono', dynamicDisabled : !record.value.isNewRecord}
          ]);
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await pmContract2Save(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
